<template>
	<view class="login">
		<image class="bg" src="../../static/kp/login-bg.png"></image>
		<!-- #ifndef MP-TOUTIAO -->
		<view class="navbar">
			<uni-nav-bar :border="false" :statusBar="true" color="#000" backgroundColor="rgba(0,0,0,0)" left-icon="left"
				@clickLeft="navBack" />
		</view>
		<!-- #endif -->
		<view class="header">
			<image class="logo" src="../../static/logo.png"></image>
			<!-- <text class="name">人才直聘</text> -->
			<!-- <text class="tips">首次验证通过即注册人才直聘账号</text> -->
		</view>
		<view class="content">
			<view class="top_click martop_app">
				<view style="display: flex;align-items: center;width: 40%;height: 80upx;flex-direction: column;" @click="typeLogin(0)">
					<text style="font-size: 40upx;">验证登录</text>
					<view :class="currentIndex == 0 ? 'bottom-select-line' : ''"></view>
				</view>
				<view style="display: flex;align-items: center;width: 40%;height: 80upx;flex-direction: column;" @click="typeLogin(1)">
					<text style="font-size: 40upx;">密码登录</text>
					<view :class="currentIndex == 1 ? 'bottom-select-line' : ''"></view>
				</view>
			</view>
			<view class="main">
				<template v-if="currentIndex==0">
					<view class="phone">
						<text class="yzb yzb-bitian" />
						<text class="tit">+86</text>
						<view class="line"></view>
						<input type="number" maxlength="11" v-model="phoneData" placeholder="请输入手机号码" />
					</view>
					<view class="code">
						<text class="yzb yzb-bitian" />
						<input type="number" maxlength="6" placeholder="请输入验证码" v-model="verCode" @confirm="toLogin"
							style="width: 60%;" />
						<view class="codeText" v-if="coding == false" @click="getCode()">获取验证码</view>
						<view class="authTime" v-else>{{ auth_time }}秒</view>
					</view>
					<!-- <view class="picker">
						<picker @change="bindPickerChange" range-key="name" :value="verCode" :range="roleList">
							<input class="padding-10" :focus="false" :disabled="true" placeholder="角色选择,首次使用请选择角色" :value="role.name" />
						</picker>
						<text class="yzb yzb-next"></text>
					</view> -->
					<!-- <view class="picker">
						<input maxlength="12" placeholder="选填,首次登录请输入邀请码" v-model="inviteCode" />
					</view> -->
				</template>
				<template v-if="currentIndex==1">
					<view class="phone">
						<text class="yzb yzb-bitian" />
						<input maxlength="50" v-model="phoneData" placeholder="请输入手机号/邮箱号" />
					</view>
					<view class="code">
						<text class="yzb yzb-bitian" />
						<input placeholder="请输入登录密码" v-model="passData" />
					</view>
				</template>
			</view>
			<view class="bottom_pact">
				<radio-group @change="radioGroup">
					<radio color="#12ae85" value="true" :checked="radioValue" style="transform:scale(0.72);"></radio>
				</radio-group>
				<view class="agreement" @click="radioValue =1">
					已阅读并同意
					<text class="agreement-text" @click.stop="toTerms">用户协议</text>
					和
					<text class="agreement-text" @click.stop="toPrivacy">隐私政策</text>
				</view>
			</view>
			<view class="btn-login" @click="startLogin()">
				<text class="name">登 录</text>
			</view>
			<!--  #ifndef H5 -->
			<!-- 其他登录 -->
			<!-- <view class="auth-login">
				<view class="others">
					<view class="line"></view>
					<text class="other">其他方式</text>
					<view class="line"></view>
				</view>
				<view class="auth-log-login" @click="navBack">
					<image src="/static/kp/wx.png" style="width: 80upx;height: 80upx;"></image>
				</view>
			</view> -->
			<!-- #endif -->
			<view class="footer">
				<navigator url="/pages/login/forgetPwd" open-type="navigate">找回密码</navigator>
				<!-- <text>|</text> -->
				<!-- <navigator url="/pages/public/reg" open-type="navigate">注册账号</navigator> -->
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				checked: false,
				radioValue: false,
				currentIndex: 0,
				isWeiXin: 0,
				isGetPhone: false,
				verCode: '',
				code: null,
				inviteCode:'',
				role:{
					code:'',
					name:''
				},
				
				first:true,
				
				coding: false,
				auth_time: 60,
				
				referrer:"",
				roleList:[],
			};
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad() {
			this.referrer=uni.getStorageSync('referrer');
			let firstLogin = uni.getStorageSync('firstLogin');
			if (firstLogin) {
				this.first = 2
			} else {
				console.log('首次登录');
				this.first = 1
			}
			console.log("first===",this.first)
			this.getAppRoles();
		},
		mounted() {
			_this = this;
		},
		methods: {
			...mapMutations(['login']),
			
			//移动端角色列表
			async getAppRoles() {
				let roles = await this.$apis.getAppRoles();
				this.roleList = roles;
			},
			
			bindPickerChange: function(e) {
				console.log("bindPickerChange==",e);
				this.role = this.roleList[e.target.value];
				console.log("this.role==",this.role);
			},

			// H5微信登录
			wechatH5Login() {
				if (this.radioValue == false) {
					uni.showToast({
						title: '请勾选用户协议',
						icon: 'none'
					});
					return false;
				}
				const that = this;
				let href = window.location.origin;
				let page = that.$api.prePage();
				let prePath = '/pages/index/index';
				if (page) {
					prePath = page.__page__.path;
				}
				window.location =
					'https://open.weixin.qq.com/connect/oauth2/authorize?' +
					'appid=' +
					Api.h5Appid +
					'&redirect_uri=' +
					escape(href) +
					'&response_type=code&scope=snsapi_userinfo&state=mallplus#wechat_redirect';
			},

			// 登录
			async startLogin() {
				//用户来源：0->Android；1->IOS 2->h5 3->微信小程序 4->支付宝小程序
				let sourceType = 0;
				//#ifdef MP-WEIXIN
				sourceType = 3;
				//#endif
				//#ifdef APP-PLUS
				if (plus.os.name == 'Android') {
					sourceType = 0;
				} else {
					sourceType = 1;
				}
				//#endif
				// 验证码登录
				if (this.currentIndex == 0) {
					let phoneReg = /^1[1-9][0-9]\d{8}$/;
					try {
						if (this.phoneData == '') {
							throw '请填写手机号';
						}
						if (!phoneReg.test(this.phoneData)) {
							throw '手机号格式有误';
						}
						if (this.verCode == '') {
							throw '请填写验证码';
						}
						if (this.radioValue == false) {
							throw '请勾选用户协议';
						}
					} catch (err) {
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: err
						});
						return;
						return;
					}
					let params = {
						phone: this.phoneData,
						code: this.verCode,
						referrer:this.referrer,
						inviteCode:this.inviteCode,
						role:this.role.code
					};
					uni.showLoading({
						title:"登陆中···"
					})
					let data = await this.$apis.memberLoginForCode(params);
					if (data) {
						this.$store.commit('SET_USERINFO', data.userInfo || {});
						// 前端自动登录
						this.$store.commit('SET_TOKEN', data.token);
						let permission = await this.$apis.getUserPermissionByToken();
						this.$store.commit('SET_TABBARS', permission.menu);
						this.$store.commit('SET_PERMISSION', permission.menu);
						this.$db.set('firstLogin', "1");
						uni.showToast({
							title: '登录成功',
							icon: 'none',
							position: 'bottom'
						});
						setTimeout(function() {
							let canNavBack = getCurrentPages()
							console.log("cannavback===",canNavBack);
							uni.hideLoading();
							if( canNavBack && canNavBack.length>1) { 
								// #ifdef MP-WEIXIN
									uni.navigateBack({
										delta: 2
									});
								// #endif
								// #ifndef MP-WEIXIN
								uni.navigateBack({
									delta: 1
								});
								// #endif
							} else {  
							   uni.switchTab({
							   	url: '/pages/user/user'
							   });
							} 
						}, 1000);
					}
				} else {
					try {
						if (this.phoneData == '') {
							throw '请填写手机号或邮箱号';
						}
						if (this.passData == '') {
							throw '请填写密码';
						}
						if (this.radioValue == false) {
							throw '请勾选用户协议';
						}
					} catch (err) {
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: err
						});
						return;
					}
					let params = {
						phone: this.phoneData,
						password: this.passData,
						referrer:this.referrer,
						inviteCode:this.inviteCode,
						role:this.role.code
					};
					uni.showLoading({
						title:"登陆中···"
					})
					let data = await this.$apis.memberLogin(params);
					if (data) {
						this.$store.commit('SET_USERINFO', data.userInfo || {});
						// 前端自动登录
						this.$store.commit('SET_TOKEN', data.token);
						let permission = await this.$apis.getUserPermissionByToken();
						this.$store.commit('SET_TABBARS', permission.menu);
						this.$store.commit('SET_PERMISSION', permission.menu);
						this.$db.set('firstLogin', "1");
						uni.showToast({
							title: '登录成功',
							icon: 'none',
							position: 'bottom'
						});
						setTimeout(function() {
							let canNavBack = getCurrentPages()
							console.log("cannavback===",canNavBack);
							uni.hideLoading();
							if( canNavBack && canNavBack.length>1) { 
								// #ifdef MP-WEIXIN
									uni.navigateBack({
										delta: 2
									});
								// #endif
								// #ifndef MP-WEIXIN
								uni.navigateBack({
									delta: 1
								});
								// #endif
							} else {  
							   uni.switchTab({
							   	url: '/pages/user/user'
							   });
							} 
						}, 1000);
					}
				}
			},
			
			/**
			 * 获取验证码
			 */
			async getCode() {
				// 判断手机号格式是否正确
				var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号码'
					});
					return false;
				}
				//设置倒计时秒
				this.auth_time = 60;
				this.coding = true;
				var auth_timetimer = setInterval(() => {
					this.auth_time--;
					if (this.auth_time < 0) {
						this.coding = false;
						clearInterval(auth_timetimer);
					}
				}, 1000);
				//获取验证码
				let params = { 
					phone: this.phoneData,
					smsmode:1,//短信模板方式  0 .登录模板、1.注册模板、2.忘记密码模板
				};
				let data = await this.$apis.getVerifyCode(params,{isRes: true});
				console.log('getVerCode===', data);
				if (data) {
					uni.showToast({title: data.message});
				}else{
					uni.showToast({title: '获取失败,请重试',icon:'none'});
				}
			},

			// 登录方式切换
			typeLogin(index) {
				console.log("typeLogin===",index);
				this.currentIndex = index;
				this.$forceUpdate();
			},

			radioGroup(e) {
				console.log("radioGroup==", e.detail);
				this.radioValue = e.detail.value;
			},

			// 返回
			navBack() {
				uni.navigateBack({
					delta: 1
				});
				// uni.switchTab({ url: '/pages/index/index' });
			},

			isLogin() {},

			toTerms() {
				this.$mRouter.push({
					route: this.$mRoutesConfig.terms
				});
			},

			toPrivacy() {
				this.$mRouter.push({
					route: this.$mRoutesConfig.privacy
				});
			},
		}
	};
</script>

<style lang="scss">
	.bg {
		width: 750upx;
		height: 438upx;
		position: absolute;
		z-index: 0;
	}

	.header {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		text-align: center;
		width: 100%;
		height: 320upx;

		.logo {
			width: 200upx;
			height: 200upx;
			z-index: 1;
			margin-top: 5upx;
		}

		.name {
			margin-top: 30upx;
			color: #000;
			font-size: 34upx;
			font-weight: bold;
			z-index: 1;
		}

		.tips {
			margin-top: 10upx;
			color: #999;
			font-size: 26upx;
			z-index: 1;
		}
	}

	.bottom_pact {
		margin-top: 30upx;
		display: flex;
		align-items: center;
		height: 70upx;
		line-height: 70upx;
		width: 90%;

		.agreement {
			font-size: 28upx;
			color: #9B9B9B;
		}

		.agreement-text {
			text-decoration: underline;
			color: #12ae85;
			padding: 0 10upx;
			font-size: 28upx;
		}
	}

	.auth-login {
		margin-top: 40upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.others{
		display: flex;
		flex-direction: row;
		align-items: center;
		.line{
			background-color: #eee;
			width: 200upx;
			height: 2upx;
		}
		.other{
			padding: 30upx;
			font-size: 28upx;
			color: #666;
		}
	}

	.auth-log-login {
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50%;
		width: 80upx;
		height: 80upx;
		position: relative;
		margin: 0 35upx;
	}

	.btn-wx {
		position: fixed;
		opacity: 0;
		border-radius: 50px;
		background: #2ba245;
		color: #fff;
		font-size: $font-lg;
	}
	
	.btn-login{
		margin-top: 30upx;
		border-radius: 30rpx;
		font-size: 35rpx;
		width: 90%;
		// background-color: $main-color;
		background-image: linear-gradient(90deg, #7FB6FF, #7FF1FF);
		color: #ffffff;
		align-items: center;
		display: flex;
		justify-content: center;
		height: 80upx;
		
		.name {
			font-size: 32upx;
		}
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		/* margin-top: 128upx; */
	}
	
	.top_click {
		display: flex;
		width: 100%;
		height: 120upx;
		align-items: center;
		justify-content: center;
		z-index: 1;
	}
	
	.top_click_xcx {
		display: flex;
		width: 100%;
		align-items: center;
		justify-content: center;
		margin-bottom: 20upx;
	}
	
	.martop_app {
	}
	
	.martop_xcx {
		margin-top: 180upx;
		display: flex;
		flex-direction: column;
	}
	
	.bottom-select-line {
		width: 50upx;
		height: 8upx;
		// margin-top: 10upx;
		border-radius: 15upx;
		background: $main-color;
		color: $main-color;
	}

	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		width: 88%;
		
		input{
			padding: 20upx;
			width: 100%;
		}
		.phone{
			display: flex;
			flex-direction: row;
			align-items: center;
			border-bottom: 1upx solid #eee;
			padding: 10upx 10upx;
			
			.line{
				height: 30upx;
				width: 2upx;
				background-color: #999;
				margin: 0 20upx;
			}
			
			.tit{
				color: #000;
			}
		}
		
		.code {
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 100%;
			box-sizing: border-box;
			justify-content: space-between;
			border-bottom: 1upx solid #eee;
			padding: 10upx 10upx;
			
			.codeText{
				color: $main-color;
			}
		}
		
		.picker {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			padding: 20upx 0 20upx 0;
			margin-left: 10upx;
			border-bottom: 1upx solid #eee;
			picker {
				font-size: $uni-font-size-lg;
				width: 100%;
			}
			.yzb-next {
				float: right;
				color: $font-color-999;
			}
		}
	}

	.tips {
		color: #999999;
		font-size: 28upx;
		margin-top: 64upx;
		margin-left: 48upx;
	}

	/* 其他登录方式 */
	.other_login {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256upx;
		text-align: center;
	}

	.login_icon {
		border: none;
		font-size: 64upx;
		margin: 0 64upx 0 64upx;
		color: rgba(0, 0, 0, 0.7);
	}

	.wechat_color {
		color: #83dc42;
	}

	.weibo_color {
		color: #f9221d;
	}

	.github_color {
		color: #24292e;
	}

	/* 底部 */
	.footer {
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
		width: 88%;
		font-size: 28upx;
		margin-top: 30upx;
		color: #999;
		text-align: center;
		height: 40upx;
		line-height: 40upx;
	}

	.footer text {
		font-size: 24upx;
		margin-left: 15upx;
		margin-right: 15upx;
	}
	
	.yzb-bitian {
		color: $font-color-red;
	}
</style>